package com.mathlearning.auth.repository;

import com.mathlearning.auth.entity.LearningVideo;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public interface LearningVideoRepository extends JpaRepository<LearningVideo, Long> {
    List<LearningVideo> findByChapterIdOrderBySortOrderAsc(Long chapterId);
    Long countByCourseId(Long courseId);

    @Query("select v from LearningVideo v where v.courseId = ?1 and v.chapterId = ?2 and v.sortOrder < ?3 order by v.sortOrder desc")
    Optional<LearningVideo> findPreviousVideo(Long courseId, Long chapterId, Integer sortOrder);

    @Query("select v from LearningVideo v where v.courseId = ?1 and v.chapterId = ?2 and v.sortOrder > ?3 order by v.sortOrder asc")
    Optional<LearningVideo> findNextVideo(Long courseId, Long chapterId, Integer sortOrder);

    @Modifying
    @Transactional
    @Query("update LearningVideo v set v.viewCount = v.viewCount + 1 where v.id = ?1")
    void incrementViewCount(Long videoId);
}




